package com.dimples.controller.system;

import com.dimples.common.annotation.OpsLog;
import com.dimples.common.entity.SecurityUser;
import com.dimples.common.enums.OpsLogTypeEnum;
import com.dimples.common.service.DimplesJwtTokenServiceImpl;
import com.dimples.common.transport.Result;
import com.dimples.common.util.HttpContextUtil;
import com.dimples.controller.view.BaseWebController;
import com.dimples.system.entity.SysMenu;
import com.dimples.system.service.SysMenuService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import cn.hutool.core.lang.tree.Tree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/6/26
 */
@Slf4j
@Api(tags = "菜单管理")
@Validated
@RestController
@RequestMapping(value = BaseWebController.API_SYSTEM_PREFIX)
@RequiredArgsConstructor
public class SysMenuController {

    private final DimplesJwtTokenServiceImpl tokenService;
    private final SysMenuService sysMenuService;

    @ApiOperation(value = "获取用户菜单")
    @ApiOperationSupport(order = 1)
    @OpsLog(value = "获取用户菜单", type = OpsLogTypeEnum.SELECT)
    @GetMapping("user/menu")
    @PreAuthorize("@auth.hasPerm('menu:view')")
    public Result<Object> findMenuByUsername() {
        SecurityUser currentUser = tokenService.getLoginUserFromToken(HttpContextUtil.getRequest());
        Object result = this.sysMenuService.findMenuByUsername(currentUser.getUsername());
        return Result.success(result);
    }

    @ApiOperation(value = "获取树形菜单")
    @ApiOperationSupport(order = 2)
    @OpsLog(value = "获取树形菜单", type = OpsLogTypeEnum.SELECT)
    @GetMapping("menu/tree")
    @PreAuthorize("@auth.hasPerm('menu:view')")
    public Result<Object> findMenuTree(@RequestParam(defaultValue = "false") boolean virtualNode) {
        List<Tree<Long>> result = this.sysMenuService.findMenuTree(virtualNode);
        return Result.success(result);
    }

    @ApiOperation(value = "获取角色树形菜单")
    @ApiOperationSupport(order = 2)
    @OpsLog(value = "获取角色树形菜单", type = OpsLogTypeEnum.SELECT)
    @GetMapping("menu/tree/{roleId}")
    @PreAuthorize("@auth.hasPerm('menu:view')")
    public Result<Object> findMenuTreeByRoleId(@PathVariable Long roleId) {
        List<Tree<Long>> result = this.sysMenuService.findMenuTreeByRoleId(roleId);
        return Result.success(result);
    }

    @ApiOperation(value = "获取菜单列表")
    @ApiOperationSupport(order = 3)
    @OpsLog(value = "获取菜单列表", type = OpsLogTypeEnum.SELECT)
    @GetMapping("menu")
    @PreAuthorize("@auth.hasPerm('menu:view')")
    public Result<SysMenu> findMenu() {
        List<SysMenu> result = this.sysMenuService.findMenu();
        return Result.success(result);
    }

    @ApiOperation(value = "根据id获取菜单")
    @ApiOperationSupport(order = 4)
    @OpsLog(value = "根据id获取菜单", type = OpsLogTypeEnum.SELECT)
    @GetMapping("menu/{menuId}")
    @PreAuthorize("@auth.hasPerm('menu:view')")
    public Result<SysMenu> findMenuById(@PathVariable Long menuId) {
        SysMenu result = this.sysMenuService.findMenuById(menuId);
        return Result.success(result);
    }

    @ApiOperation(value = "根据角色id获取菜单")
    @ApiOperationSupport(order = 5)
    @OpsLog(value = "根据角色id获取菜单", type = OpsLogTypeEnum.SELECT)
    @GetMapping("menu/role/{roleId}")
    @PreAuthorize("@auth.hasPerm('menu:view')")
    public Result<SysMenu> findMenuByRoleId(@PathVariable Long roleId) {
        List<SysMenu> result = this.sysMenuService.findMenuByRoleId(roleId);
        return Result.success(result);
    }

    @ApiOperation(value = "根据id更新菜单")
    @ApiOperationSupport(order = 6)
    @OpsLog(value = "根据id更新菜单", type = OpsLogTypeEnum.UPDATE)
    @PutMapping("menu")
    @PreAuthorize("@auth.hasPerm('menu:edit')")
    public Result<SysMenu> updateMenuById(@Valid @RequestBody SysMenu menuId) {
        this.sysMenuService.updateMenuById(menuId);
        return Result.success();
    }

    @ApiOperation(value = "根据id删除菜单")
    @ApiOperationSupport(order = 7)
    @OpsLog(value = "根据id删除菜单", type = OpsLogTypeEnum.DELETE)
    @DeleteMapping("menu/{menuId}")
    @PreAuthorize("@auth.hasPerm('menu:delete')")
    public Result<SysMenu> deleteMenuById(@PathVariable @NotNull(message = "不能为空") Long menuId) {
        this.sysMenuService.deleteMenuById(menuId);
        return Result.success();
    }

    @ApiOperation(value = "根据id批量删除菜单")
    @ApiOperationSupport(order = 8)
    @OpsLog(value = "根据id批量删除菜单", type = OpsLogTypeEnum.DELETE)
    @DeleteMapping("menu/batch/{menuIds}")
    @PreAuthorize("@auth.hasPerm('menu:delete')")
    public Result<SysMenu> deleteBatchMenuById(@PathVariable @NotBlank(message = "不能为空") String menuIds) {
        this.sysMenuService.deleteBatchMenuById(menuIds);
        return Result.success();
    }

    @ApiOperation(value = "新增菜单")
    @ApiOperationSupport(order = 9)
    @OpsLog(value = "新增菜单", type = OpsLogTypeEnum.ADD)
    @PostMapping("menu")
    @PreAuthorize("@auth.hasPerm('menu:add')")
    public Result<SysMenu> saveMenu(@Valid @RequestBody SysMenu sysMenu) {
        this.sysMenuService.saveMenu(sysMenu);
        return Result.success();
    }

}
